﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using SharePointContentGenerator.ContentGeneratorLogic.SharePointWebServiceCopy;

namespace SharePointContentGenerator.ContentGeneratorLogic.ContentGenerators
{
    internal class SharePointDocumentLibraryController : SharePointLibraryController
    {
        internal const int DOCUMENT_LIBRARY_TEMPLATE_ID = 101;
        internal const string DOCLIB_NEWCOMMAND = "<Batch OnError=\"Continue\" ListVersion=\"1\"><Method ID=\"1\" Cmd=\"New\"><Field Name=\"ID\">New</Field><Field Name=\"Title\">{0}</Field></Method></Batch>";

        internal SharePointDocumentLibraryController(string siteCollection) : base(siteCollection)
        {
        }

        internal SharePointDocumentLibraryController(string siteCollection, string domain, string username, string password) : base(siteCollection, domain, username, password)
        {
        }

        internal List<string> CreateDocumentLibraries(string baseLibraryName, int numberOfLibraries, bool deleteIfExist)
        {
            return base.CreateLibraries(baseLibraryName, DOCUMENT_LIBRARY_TEMPLATE_ID, numberOfLibraries, deleteIfExist);
        }

        internal void PopulateLibrariesWithDocuments(List<string> listNameList, int minRandomDocument, int maxRandomdocument)
        {
            Random rand = new Random(DateTime.Now.Second);

            foreach (string listName in listNameList)
            {
                int numberOfDocumentsToGenerate = rand.Next(minRandomDocument, maxRandomdocument);
                for (int counter = 0; counter < numberOfDocumentsToGenerate; counter++)
                {
                    string newfilename = string.Format("Document_{0}.docx", Guid.NewGuid().ToString().Substring(0, 6));
                    string documentDestinationUrl = string.Format("{0}/{1}/{2}", SiteCollection, listName, newfilename);
                    string[] destinationUrlList = { documentDestinationUrl };

                    CopyResult copyResult = new CopyResult();
                    CopyResult[] copyResults = { copyResult };

                    FieldInformation documentDescriptionFieldInfo = new FieldInformation()
                    {
                        DisplayName = "Description",
                        Type = FieldType.Text,
                        Value = "This is the test description for file " + newfilename

                    };
                    FieldInformation documentTitleFieldInfo = new FieldInformation()
                    {
                        DisplayName = "Title",
                        Type = FieldType.Text,
                        Value = "Autogenerated " + newfilename.Replace(".docx", string.Empty)

                    };
                    FieldInformation[] fieldInfoArray = { documentDescriptionFieldInfo, documentTitleFieldInfo };

                    WordDocumentGenerator docController = new WordDocumentGenerator();
                    MemoryStream objStream = docController.CreateWordDocument();
                    byte[] contentInBytes = new byte[objStream.Length];
                    objStream.Position = 0;
                    objStream.Read(contentInBytes, 0, Convert.ToInt32(objStream.Length));

                    uint copyresult = CopyClient.CopyIntoItems(newfilename, destinationUrlList, fieldInfoArray, contentInBytes, out copyResults);

                    string m = copyResults[0].ErrorMessage;
                }
            }
        }
    }
}
